﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;

using Revis.VClinicNet.App.Internal;
using Revis.VClinicNet.App.Internal.Dto;
using Revis.VClinicNet.App.Internal.Dto.Validation;
using Revis.VClinicNet.UI.Internal.Mvc.Extensions;
using Revis.VClinicNet.UI.Internal.Mvc.Models;
using Revis.VClinicNet.UI.Internal.Mvc.Infrastructure;
using Revis.VClinicNet.Core.Domain.Credentials;

namespace Revis.VClinicNet.UI.Internal.Mvc.Controllers
{
    public class DoctorController : Infrastructure.ControllerBase
    {
        private IDoctorService _doctorService = null;
        private IVisitService _visitService = null;

        public DoctorController(IDoctorService ids, IVisitService ivs)
        {
            _doctorService = ids;
            _visitService = ivs;
        }

        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Doctor | WorkerUserRole.Recepcionist)]
        public ActionResult Detail(long id)
        {
            DoctorDetailDto doctor = null;
            try {
                doctor = _doctorService.Find<DoctorDetailDto>(id);
            } catch (ClinicException ce) {
                return View("Error", new HandleErrorInfo(ce, "Doctor", "Detail"));
            }
            return View(doctor);
        }



        [HttpPost]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Doctor | WorkerUserRole.Recepcionist)]
        public JsonResult JsonWorkInfo(long id, string date)
        {
            JsonWorkInfoModel model = null;
            try {
                DateTime dateTime = DateTime.Parse(date);
                DoctorWorkInfoDto workInfo = _doctorService.FindWorkInfo(id, dateTime);
                model = new JsonWorkInfoModel(workInfo);
            } catch (Exception) {
                model = new JsonWorkInfoModel();
            }
            return Json(model, JsonRequestBehavior.AllowGet);
        }
    }
}
